<template>
  <el-cascader
    :value="value"
    :props="props"
    :options="optionsData"
    separator="-"
    @change="handleChange"
  />
</template>

<script>
import { getDepartmentAPI } from '@/api/department'
import { transListToTreeData } from '@/utils/index'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      selectValue: [],
      optionsData: [],
      props: {
        value: 'id',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取数据
    async getDepartment() {
      this.optionsData = transListToTreeData(await getDepartmentAPI(), 0)
    },
    // 选择数据发生变换的时候触发
    handleChange(selectValue) {
      // 修改details组件里的departmentId的数值
      if (selectValue.length > 0) {
        this.$emit('input', selectValue[selectValue.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style>
</style>
